package mm.chenchen.modules.host.service.impl;

import ch.ethz.ssh2.Connection;
import io.swagger.annotations.ApiParam;
import mm.chenchen.constant.ResultMsg;

import mm.chenchen.constant.StatusCode;
import mm.chenchen.modules.host.entity.model.HostInfoDetailRes;
import mm.chenchen.modules.host.entity.model.SshCommandModel;
import mm.chenchen.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mm.chenchen.modules.host.entity.HostInfo;
import mm.chenchen.modules.host.dao.HostInfoDao;
import mm.chenchen.modules.host.service.HostInfoService;

import java.net.InetAddress;
import java.util.Date;
import java.util.List;
import java.util.Map;

import mm.chenchen.modules.host.dao.model.request.HostInfoQuery;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;


@Service("hostInfoService")
public class HostInfoServiceImpl extends ServiceImpl<HostInfoDao, HostInfo> implements HostInfoService {


    public R findPage(Page<Map> page, HostInfoQuery param) {
        IPage<HostInfo> pageData = baseMapper.findPage(page, param);
        if (pageData != null) {
            List<HostInfo> list = pageData.getRecords();
            if (list != null) {
                list.forEach(ele -> {
                    if (StatusCode.VALID.equals(ele.getValid())) {
                        if (SshUtils.Connectable(ele.getIp(), ele.getUser(), ele.getPassword())) {
                            ele.setStatus(SshUtils.CONNECTABLE);
                        } else {
                            ele.setStatus(SshUtils.NOT_CONNECTABLE);
                        }
                    } else {
                        ele.setStatus(SshUtils.NOT_CONNECTABLE);
                    }
                });
            }
            return R.ok().setData(new PageModel(pageData.getRecords(), pageData.getTotal(), pageData.getSize(), pageData.getCurrent()));
        } else {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
    }

    public R getById(Long id) {
        return R.ok().setData(super.getById(id));
    }


    public R saveInfo(HostInfo param) {
        param.setCreateTime(new Date());
        return R.ok().setData(super.save(param));
    }


    public R update(HostInfo param) {
        param.setUpdateTime(new Date());
        HostInfo data = super.getById(param.getId());
        if (ObjectUtils.isEmpty(data)) {
            return R.error(ResultMsg.DATA_NOT_EXIT);
        }
        return R.ok().setData(super.updateById(param));
    }


    public R deleteById(Long id) {
        return R.ok().setData(super.removeById(id));
    }

    public R environmentDetail(Long id) throws Exception{
        HostInfo hostInfo = super.getById(id);
        if (ObjectUtils.isEmpty(hostInfo)) {
            return R.error();
        }
        if (NetUtils.Connectable(hostInfo.getIp())){
            return R.error("无法ping通此ip");
        }
        HostInfoDetailRes res = new HostInfoDetailRes();
        Connection connection = SshUtils.login(hostInfo.getIp(), hostInfo.getUser(), hostInfo.getPassword());
        if (ObjectUtils.isEmpty(connection)) {
            return R.error(ResultMsg.NO_SSH_CONNECTION);
        }
        res.setJavaVersion(HostEnvironmentUtils.javaVersion(connection));
        res.setJavaThread(HostEnvironmentUtils.getThreads(connection,"java"));

        res.setPythonVersion(HostEnvironmentUtils.python3Version(connection));
        res.setPythonThread(HostEnvironmentUtils.getThreads(connection,"python3"));

        res.setNodeVersion(HostEnvironmentUtils.nodeVersion(connection));
        res.setNodeThread(HostEnvironmentUtils.getThreads(connection,"node"));
        return R.ok().setData(res);
    }

    @Override
    public R sshCommand(SshCommandModel param) {
        HostInfo hostInfo = super.getById(param.getHostId());
        Connection connection = SshUtils.login(hostInfo.getIp(), hostInfo.getUser(), hostInfo.getPassword());
        return R.ok().setData(SshUtils.execute(connection,param.getSshCommand()));
    }
}